package sf.view.fields;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JTextField;

import sf.model.Context;
import sf.model.fields.AbstractFieldModel;
import sf.model.fields.ZoomFieldModel;
import sf.model.fields.event.FieldChangeEvent;
import sf.view.StandardForm;
import sf.view.StandardFormFactory;

public class ZoomField extends AbstractField {
	private static final long serialVersionUID = -582953764974172776L;

	/** polje za unos i izmenu kljuceva. */
	private JTextField inputField;
	/** dugme za zumiranje. */
	private JButton browseButton;

	/*
	 * NOTE Stavio sam kontroler zato sto ce neka polja, kao sto je ovo, morati
	 * da imaju neke finije akcije. Npr parsiranje i maskiranje textfielda za
	 * kompozitne kljuceve... Za sada ovaj kod samo
	 * 
	 */
	private class ZoomFieldController implements ActionListener, FocusListener {
		ZoomField view;

		ZoomFieldController(ZoomField view) {
			this.view = view;
		}

		public void actionPerformed(ActionEvent e) {
			Context parentContext = ((StandardForm) view.getTopLevelAncestor())
					.getContext();

			Context context = new Context(view);
			context.setMode(parentContext.getMode() | StandardForm.FROM_ZOOM);
			context
					.setTable(((ZoomFieldModel) getModel())
							.getParentTableCode());
			HashMap<String, Object> filter = (HashMap<String, Object>) ((ZoomFieldModel) getModel())
					.getFilterFields();
			context.setParam("filter", filter);
			context.setParam("primaryTargetFieldCode",
					((ZoomFieldModel) getModel()).getPrimaryTargetField());
			context.setParam("primaryTargetFieldValue",
					((ZoomFieldModel) getModel()).getPrimaryTargetFieldValue());
			StandardFormFactory.createAndShow(context, view);
		}

		public void focusGained(FocusEvent e) {
			// TODO Auto-generated method stub

		}

		public void focusLost(FocusEvent e) {
			updateValue();

		}

		private void updateValue() {
			if (((ZoomFieldModel) getModel()).check(inputField.getText())
					&& !inputField.getText().equals(getModel().getValue())) {
				model.setValue(inputField.getText());
			}
		}

	}

	private ZoomFieldModel model;
	private ZoomFieldController controller;

	public ZoomField() {
		model = new ZoomFieldModel();
		controller = new ZoomFieldController(this);
		model.addChangeListener(this); // FIXME

		/*
		 * JLabel label = new JLabel(getLabelText()); add(label,
		 * BorderLayout.WEST);
		 */

		if (model.getSize() > 0)
			inputField = new JTextField(model.getSize());
		else
			inputField = new JTextField();
		inputField.addFocusListener(controller);
		add(inputField, BorderLayout.CENTER);

		browseButton = new JButton("...");
		browseButton.addActionListener(controller);
		add(browseButton, BorderLayout.EAST);
	}

	public AbstractFieldModel getModel() {
		return model;
	}

	public void setModel(AbstractFieldModel model) {
		this.model = (ZoomFieldModel) model;
		inputField.setText((String) this.model.getValueString());
		inputField.setColumns(this.model.getSize());
		this.model.addChangeListener(this);
	}

	public void changePerformed(FieldChangeEvent event) {
		inputField.setText(((String) model.getValue()).trim());
	}

	public Object getValue() {
		return model.getValue();
	}

	public void setValue(Object value) {
		model.setValue(value);
	}

	public void setEditable(boolean editable) {
		super.setEnabled(editable);
		inputField.setEditable(editable);
		browseButton.setEnabled(editable);
	}

}
